<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			#test{
				width: 50px;
				height: 50px;
				border: 1px solid black;
				background-color: red;
			}
		</style>
	</head>
	<body>
		<div id="test"></div>
		
		<script type="text/javascript">
			var ab = document.getElementById("test");
			/*function fun(){
				alert("test");
			}
			
			ab.onclick = fun; //此处赋值不加括号，只赋值方法名*/
			var flag = true;
			
			ab.onclick = function(){
				if(flag){
					ab.style.backgroundColor = "blue"; //元素属性设置
					flag = false;
				}else{
					ab.style.backgroundColor = "red";
					flag = true;
				}
			}
			
		</script>
	</body>
</html>
